package com.bjx.flow.mmx.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.flow.mmx.domain.entity.MmxFlowVariablesEntity;
import com.bjx.flow.mmx.mapper.MmxFlowVariablesMapper;
import com.bjx.flow.mmx.service.MmxFlowVariablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @desc: 流程变量服务实现
 * @author: liushuai
 * @date: 2023/05/22
 */
@Service
public class MmxFlowVariablesServiceImpl extends ServiceImpl<MmxFlowVariablesMapper, MmxFlowVariablesEntity> implements MmxFlowVariablesService {
    @Autowired
    private MmxFlowVariablesMapper flowVariablesMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setFlowVariables(Long instanceId, Map variables) {
        this.setFlowVariables(instanceId, instanceId, variables);
    }

    @Transactional
    @Override
    public void setFlowVariables(Long instanceId, Long branchId, Map variables) {
        LambdaQueryWrapper<MmxFlowVariablesEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MmxFlowVariablesEntity::getInstanceId, instanceId);
        queryWrapper.eq(MmxFlowVariablesEntity::getBranchId, branchId);
        flowVariablesMapper.delete(queryWrapper);
        JSONObject json = Convert.convert(JSONObject.class, variables);
        flowVariablesMapper.insert(MmxFlowVariablesEntity.builder()
                .instanceId(instanceId)
                .branchId(branchId)
                .variables(json.toString())
                .build());

    }
}
